package views;

import model.Problem;
import model.Solution;
import model.SolverEnvironment;
import processing.core.PApplet;

public class VisualizeBalanceCostApplet extends PApplet {
	final static int machine_width = 10;
	final static int machine_height = 50;

	SolverEnvironment env;
	Problem problem;
	public Solution currentSolution;
	long max_cost_contribution = Long.MIN_VALUE;

	public VisualizeBalanceCostApplet(SolverEnvironment env) {
		this.env = env;
		this.problem = env.getProblem();
		this.currentSolution = env.getCurrentSolution();
	}

	public void setup() {
		int width = problem.M * machine_width, height = machine_height;
		size(width, height);
		background(255, 255, 255);
	}

	public void draw() {
		if (currentSolution.balance_cost_max_contribution_value > -1L) {
			if (currentSolution.balance_cost_max_contribution_value > max_cost_contribution)
				max_cost_contribution = currentSolution.balance_cost_max_contribution_value;
			for (int i = 0; i < problem.M; i++) {
				if (currentSolution.balance_cost_contribution_zipped[i] > 0) {
					long n = currentSolution.balance_cost_contribution_zipped[i];
					int r = (int) (255 * n / (double) max_cost_contribution);
					int g = (int) ((255 * (max_cost_contribution - n)) / (double) max_cost_contribution);
					fill(r, g, 0);
				} else
					fill(0, 255, 255);
				int x = i * machine_width;
				int width = machine_width;
				rect(x, 0, width, machine_height);
			}
			if (mousePressed) {
				int machine = mouseX / machine_width;
				if ((machine < problem.M) && (machine >= 0)) {
					String msg = String
							.format("m%d=%,d",
									machine,
									currentSolution.balance_cost_contribution_zipped[machine]);
					// System.err.println(msg);
					fill(0, 0, 0);
					text(msg, mouseX, mouseY);
				}
			}
		}
	}

	public void setCurrentSolution(Solution current_solution) {
		this.currentSolution = current_solution;
	}
}
